{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fitting Functions to Real Data\n",
    "\n",
    "In this notebook, we shall download measurements of carbon dioxide in the atmosphere from 1958 to 2019. Your task will be to fit a function to this data in order to predict what the $CO_2$ concentration in the atmosphere is going to be in 2050. Our first step is to download the data, which we do below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.optimize import curve_fit\n",
    "import numpy as np\n",
    "from plotter import plot_fit\n",
    "%matplotlib inline\n",
    "\n",
    "# Here is the url source to a file which contains the measurements\n",
    "url = \"ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_mm_mlo.txt\"\n",
    "\n",
    "# Here we dowload the file, ignore the headers, and specify the data is separated by spaces\n",
    "df = pd.read_csv(url, skiprows=72, sep='\\s+').reset_index(drop=True)\n",
    "# Here we rename the columns to something more convenient \n",
    "df.columns = [\"year\", \"month\", \"decimal_year\", \"co2_av\", \"co2_interp\", \"co2_trend\", \"ignore\"]\n",
    "# Missing data is filled in with -99.99, so we simply get rid of it \n",
    "df = df[~df['co2_av'].isin([-99.99])]\n",
    "# Removing a column we don't need \n",
    "del df['ignore']\n",
    "# Here we view the first five entries of the table \n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where in the data above, the only data we need to be concerned with is the `decimal_year` column and the `co2_av` column. Let's plot those and see what they look like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = df.plot(x='decimal_year', y='co2_av', kind = 'scatter', figsize =(12,8), s=4)\n",
    "ax.set_xlabel(\"Year\" , size = 16)\n",
    "ax.set_ylabel(\"$CO_2$ (PPM)\", size = 16)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From the plot above, you probably notice that there's an increasing trend and some periodic behavior. Your task is to fit a function to this data. Feel free to make _any_ transformations you see fit to the data and fit _any_ function you wish to the data as well. Feel free to try multiple. Be creative! \n",
    "\n",
    "\n",
    "## Getting Started\n",
    "\n",
    "Below we set up some place holder code for you to get working on this task. Good Luck!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Here we extract only the data we want from the table .\n",
    "x_data = df['decimal_year']\n",
    "y_data = df['co2_av']\n",
    "\n",
    "# Feel free to transform this data as you see fit! \n",
    "# For example, if you wanted to take the square root of the entire column, you could type\n",
    "# x_data_sqrt = np.sqrt(x_data)\n",
    "\n",
    "# Create your function to fit here!\n",
    "def co2_fit(x, YOUR_OTHER_ARGUMENTS_HERE):\n",
    "    return # Your function here\n",
    "\n",
    "\n",
    "\n",
    "values, fit_quality  = curve_fit(co2_fit, x_data, y_data)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
